package org.marsson.bNav;

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;


/**
 * @author Marsson
 *
 */
class GridArea extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	protected Pedaco area[][];
	public boolean vertical = false;
	private String title;
	private Point selected;
	protected Point cursorLocation;
	private Rectangle gridRects[][]; 
	protected PlayingField mainHandle;
	

	

	/**
	 * Constructor for generic grid
	 * @param title
	 * @param mainHandle
	 * @param size
	 */
	
	public GridArea(String title, PlayingField mainHandle, int size) {
		this.title = title;
		this.mainHandle = mainHandle;
		this.area=new Pedaco [size][size];
		this.gridRects= new Rectangle[size][size];
		
		//Generates the array...
		for (int i=0;i<size;i++){
			for(int j =0;j<size;j++){
				
				area[i][j]=new Pedaco();
			}
		}
		
		for (int x = 0; x < size; x++)	{
			for (int y = 0; y < size; y++){
				gridRects[x][y] = new Rectangle(x * 25, y * 25, 25, 25);
			}}

		addMouseMotionListener(new MouseMovingHandler(this.gridRects.length));
		addMouseListener(new MouseHandler());

		setOpaque(false);
	}

	
	
	/**
	 * @return
	 */
	public Point getSelected() {
		Point temp = selected;
		selected = null;
	
		return temp;
	}
	
	public void setShip(Point point, int value, boolean vertical ,int size){
		if(vertical){
		
		}else{
			
		}
	}
	/* (non-Javadoc)
	 * @see javax.swing.JComponent#getPreferredSize()
	 */
	public Dimension getPreferredSize() {
		return new Dimension(25*area.length +1, 25*area.length+20);
	}

	/**
	 * @param where
	 * @param contents
	 */
	public void setArea(Point where, Pedaco pedaco) {
		area[(int) where.getX()][(int) where.getY()] = pedaco;
	}

	
	/**
	 * @param check
	 * @return
	 */
	public Pedaco getArea(Point check) {
		return area[(int) check.getX()][(int) check.getY()];
	}

	/**
	 * @return
	 */
	protected boolean validPlacement() {
		if (vertical) {

			if ((int) cursorLocation.getX() + mainHandle.enumShip.size > this.gridRects.length) {
				return false;
			}
			// verifica se ha overlap vertical:
			Pedaco pedaco;
			for (int i = 0; i < mainHandle.enumShip.size; i++) {
				pedaco = area[(int) cursorLocation.getX() + i][(int) cursorLocation
						.getY()];

				// System.out.printf("Ponto[%i][%i] teste vale:%i",(int)cursorLocation.getX(),(int)cursorLocation.getY(),teste);
				if (pedaco.getNavio() != Ships.SPLASH) {
					return false;
				}
			}
		} else {

			if ((int) cursorLocation.getY() + mainHandle.enumShip.size > this.gridRects.length) {
				return false;
			}

			Pedaco pedaco;
			for (int i = 0; i < mainHandle.enumShip.size; i++) {
				pedaco = area[(int) cursorLocation.getX()][(int) cursorLocation
						.getY()
						+ i];

				if (pedaco.getNavio() != Ships.SPLASH) {
					return false;
				}
			}
		}

		return true;
	}
	
	/* (non-Javadoc)
	 * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
	 */
	public void paintComponent(Graphics g) {
		Graphics2D g2 = (Graphics2D) g;
		int sqSize = 25;
		GradientPaint gp = new GradientPaint(0.0f, 0.0f,
				new Color(40, 200, 140), 250.0f, 250.0f,
				new Color(40, 180, 210));
		g2.setPaint(gp);
		g2.fillRect(0, 0, sqSize*this.gridRects.length, sqSize*this.gridRects.length);

		g2.setColor(new Color(0, 100, 90));
		for (int i = 1; i < this.gridRects.length; i++) {
			g2.drawLine(i * sqSize, 0, i * sqSize, sqSize*this.gridRects.length);
			g2.drawLine(0, i * sqSize,sqSize*this.gridRects.length, i * sqSize);
		}
		g2.setColor(Color.black);
		g2.draw3DRect(0, 0, sqSize*this.gridRects.length, sqSize*this.gridRects.length, false);

		g2.setColor(new Color(0, 60, 60));
		g2.drawString(title, sqSize*this.gridRects.length/2 - (title.length() * 4), sqSize*this.gridRects.length+18);
	}

	/**
	 * @author Marsson
	 *
	 */
	private class MouseMovingHandler extends MouseMotionAdapter {
		private int size;
		
		public MouseMovingHandler(int size){
			this.size = size;
			
		}
		private Rectangle lastSelected = new Rectangle();

		public void mouseMoved(MouseEvent e) {
			int y = (int) (e.getPoint().getX() / 25);
			int x = (int) (e.getPoint().getY() / 25);

			if (y < size && x < size && gridRects[x][y] != lastSelected) {
				lastSelected = gridRects[x][y];
				cursorLocation = new Point(x, y);
				repaint();
			
			}
		}
	}

	/**
	 * @author Marsson
	 *
	 */
	private class MouseHandler extends MouseAdapter {
	
		public void mousePressed(MouseEvent e) {
			if (e.getModifiers() == e.BUTTON1_MASK) {
				selected = cursorLocation;
				//mainHandle.addMessage("Voce selecionou: " + selected);
				if(mainHandle.mode==2)
				{mainHandle.lastclicked=selected;}
				repaint();
				
			}
			if (e.getModifiers() == e.BUTTON3_MASK) {
				vertical = !vertical; // toggles vertical ship placing state
				
				repaint();
			}
		}
	}
 
	

}